package hva.team2.viavia.tools;
import android.provider.MediaStore;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


/**
 * Created by sempere on 29/07/13.
 *
 * //http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
 */
public class CityParser {

    // XML node keys
    static final String KEY_CITY = "city";
    static final String KEY_PLACE = "place";
    static final String KEY_NAME = "name"; // parent node
    static final String KEY_TYPE = "type";
    static final String KEY_LATITUDE = "latitude";
    static final String KEY_LONGITUDE = "longitude";
    static final String KEY_DESCRIPTION = "description";

    public CityParser(){

    }


    private Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        // return DOM
        return doc;
    }


    private String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }

    private final String getElementValue( Node elem ) {
        Node child;
        if( elem != null){
            if (elem.hasChildNodes()){
                for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                    if( child.getNodeType() == Node.TEXT_NODE  ){
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    public City parse(String XML){

        City city= new City();
        String content;
        try{
            content = new Scanner(new File(XML)).useDelimiter("\\Z").next();
        } catch (Exception ex){
            content = "";
        }

        Document doc = getDomElement(content);
        Element e;

        Log.e("xml",XML);
        NodeList nl = doc.getElementsByTagName(KEY_CITY);
        e = (Element) nl.item(0);
        city.name = getValue(e, KEY_NAME);

        nl = doc.getElementsByTagName(KEY_PLACE);
        for (int i = 0; i < nl.getLength(); i++) {
            e= (Element) nl.item(i);
            Place p = new Place();
            p.name = getValue(e, KEY_NAME);
            p.type = getValue(e, KEY_TYPE);
            p.description = getValue(e, KEY_DESCRIPTION);
            p.latitude = Float.parseFloat(getValue(e,KEY_LATITUDE));
            p.longitude = Float.parseFloat(getValue(e,KEY_LONGITUDE));
            city.places.add(p);
        }

        return city;
    }



}
